<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "https://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/xhtml;charset=UTF-8"/>
<meta http-equiv="X-UA-Compatible" content="IE=9"/>
<meta name="generator" content="Doxygen 1.9.2"/>
<meta name="viewport" content="width=device-width, initial-scale=1"/>
<title>GrPPI: Generic Build Instructions</title>
<link href="tabs.css" rel="stylesheet" type="text/css"/>
<script type="text/javascript" src="jquery.js"></script>
<script type="text/javascript" src="dynsections.js"></script>
<link href="search/search.css" rel="stylesheet" type="text/css"/>
<script type="text/javascript" src="search/searchdata.js"></script>
<script type="text/javascript" src="search/search.js"></script>
<link href="doxygen.css" rel="stylesheet" type="text/css" />
</head>
<body>
<div id="top"><!-- do not remove this div, it is closed by doxygen! -->
<div id="titlearea">
<table cellspacing="0" cellpadding="0">
 <tbody>
 <tr style="height: 56px;">
  <td id="projectlogo"><img alt="Logo" src="logo.svg"/></td>
  <td id="projectalign" style="padding-left: 0.5em;">
   <div id="projectname">GrPPI
   &#160;<span id="projectnumber">1.0</span>
   </div>
   <div id="projectbrief">Generic and Reusable Parallel Pattern Interface</div>
  </td>
 </tr>
 </tbody>
</table>
</div>
<!-- end header part -->
<!-- Generated by Doxygen 1.9.2 -->
<script type="text/javascript">
/* @license magnet:?xt=urn:btih:cf05388f2679ee054f2beb29a391d25f4e673ac3&amp;dn=gpl-2.0.txt GPL-v2 */
var searchBox = new SearchBox("searchBox", "search",false,'Search','.html');
/* @license-end */
</script>
<script type="text/javascript" src="menudata.js"></script>
<script type="text/javascript" src="menu.js"></script>
<script type="text/javascript">
/* @license magnet:?xt=urn:btih:cf05388f2679ee054f2beb29a391d25f4e673ac3&amp;dn=gpl-2.0.txt GPL-v2 */
$(function() {
  initMenu('',true,false,'search.php','Search');
  $(document).ready(function() { init_search(); });
});
/* @license-end */</script>
<div id="main-nav"></div>
<!-- window showing the filter options -->
<div id="MSearchSelectWindow"
     onmouseover="return searchBox.OnSearchSelectShow()"
     onmouseout="return searchBox.OnSearchSelectHide()"
     onkeydown="return searchBox.OnSearchSelectKey(event)">
</div>

<!-- iframe showing the search results (closed by default) -->
<div id="MSearchResultsWindow">
<iframe src="javascript:void(0)" frameborder="0" 
        name="MSearchResults" id="MSearchResults">
</iframe>
</div>

</div><!-- top -->
<div class="PageDoc"><div class="header">
  <div class="headertitle">
<div class="title">Generic Build Instructions </div>  </div>
</div><!--header-->
<div class="contents">
<div class="textblock"><h3><a class="anchor" id="autotoc_md265"></a>
Setup</h3>
<p>To build GoogleTest and your tests that use it, you need to tell your build system where to find its headers and source files. The exact way to do it depends on which build system you use, and is usually straightforward.</p>
<h2><a class="anchor" id="autotoc_md266"></a>
Build with CMake</h2>
<p>GoogleTest comes with a CMake build script (<a href="https://github.com/google/googletest/blob/master/CMakeLists.txt">CMakeLists.txt</a>) that can be used on a wide range of platforms ("C" stands for cross-platform.). If you don't have CMake installed already, you can download it for free from <a href="http://www.cmake.org/">http://www.cmake.org/</a>.</p>
<p>CMake works by generating native makefiles or build projects that can be used in the compiler environment of your choice. You can either build GoogleTest as a standalone project or it can be incorporated into an existing CMake build for another project.</p>
<h3><a class="anchor" id="autotoc_md267"></a>
Standalone CMake Project</h3>
<p>When building GoogleTest as a standalone project, the typical workflow starts with</p>
<div class="fragment"><div class="line">git clone https://github.com/google/googletest.git -b release-1.10.0</div>
<div class="line">cd googletest        # Main directory of the cloned repository.</div>
<div class="line">mkdir build          # Create a directory to hold the build output.</div>
<div class="line">cd build</div>
<div class="line">cmake ..             # Generate native build scripts for GoogleTest.</div>
</div><!-- fragment --><p>The above command also includes GoogleMock by default. And so, if you want to build only GoogleTest, you should replace the last command with</p>
<div class="fragment"><div class="line">cmake .. -DBUILD_GMOCK=OFF</div>
</div><!-- fragment --><p>If you are on a *nix system, you should now see a Makefile in the current directory. Just type <code>make</code> to build GoogleTest. And then you can simply install GoogleTest if you are a system administrator.</p>
<div class="fragment"><div class="line">make</div>
<div class="line">sudo make install    # Install in /usr/local/ by default</div>
</div><!-- fragment --><p>If you use Windows and have Visual Studio installed, a <code>gtest.sln</code> file and several <code>.vcproj</code> files will be created. You can then build them using Visual Studio.</p>
<p>On Mac OS X with Xcode installed, a <code>.xcodeproj</code> file will be generated.</p>
<h3><a class="anchor" id="autotoc_md268"></a>
Incorporating Into An Existing CMake Project</h3>
<p>If you want to use GoogleTest in a project which already uses CMake, the easiest way is to get installed libraries and headers.</p>
<ul>
<li>Import GoogleTest by using <code>find_package</code> (or <code>pkg_check_modules</code>). For example, if <code>find_package(GTest CONFIG REQUIRED)</code> is succeed, you can use the libraries as <code>GTest::gtest</code>, <code>GTest::gmock</code>.</li>
</ul>
<p>And a more robust and flexible approach is to build GoogleTest as part of that project directly. This is done by making the GoogleTest source code available to the main build and adding it using CMake's <code>add_subdirectory()</code> command. This has the significant advantage that the same compiler and linker settings are used between GoogleTest and the rest of your project, so issues associated with using incompatible libraries (eg debug/release), etc. are avoided. This is particularly useful on Windows. Making GoogleTest's source code available to the main build can be done a few different ways:</p>
<ul>
<li>Download the GoogleTest source code manually and place it at a known location. This is the least flexible approach and can make it more difficult to use with continuous integration systems, etc.</li>
<li>Embed the GoogleTest source code as a direct copy in the main project's source tree. This is often the simplest approach, but is also the hardest to keep up to date. Some organizations may not permit this method.</li>
<li>Add GoogleTest as a git submodule or equivalent. This may not always be possible or appropriate. Git submodules, for example, have their own set of advantages and drawbacks.</li>
<li>Use CMake to download GoogleTest as part of the build's configure step. This is just a little more complex, but doesn't have the limitations of the other methods.</li>
</ul>
<p>The last of the above methods is implemented with a small piece of CMake code in a separate file (e.g. <code>CMakeLists.txt.in</code>) which is copied to the build area and then invoked as a sub-build <em>during the CMake stage</em>. That directory is then pulled into the main build with <code>add_subdirectory()</code>. For example:</p>
<p>New file <code>CMakeLists.txt.in</code>:</p>
<div class="fragment"><div class="line">cmake_minimum_required(VERSION 2.8.12)</div>
<div class="line"> </div>
<div class="line">project(googletest-download NONE)</div>
<div class="line"> </div>
<div class="line">include(ExternalProject)</div>
<div class="line">ExternalProject_Add(googletest</div>
<div class="line">  GIT_REPOSITORY    https://github.com/google/googletest.git</div>
<div class="line">  GIT_TAG           master</div>
<div class="line">  SOURCE_DIR        &quot;${CMAKE_CURRENT_BINARY_DIR}/googletest-src&quot;</div>
<div class="line">  BINARY_DIR        &quot;${CMAKE_CURRENT_BINARY_DIR}/googletest-build&quot;</div>
<div class="line">  CONFIGURE_COMMAND &quot;&quot;</div>
<div class="line">  BUILD_COMMAND     &quot;&quot;</div>
<div class="line">  INSTALL_COMMAND   &quot;&quot;</div>
<div class="line">  TEST_COMMAND      &quot;&quot;</div>
<div class="line">)</div>
</div><!-- fragment --><p>Existing build's <code>CMakeLists.txt</code>:</p>
<div class="fragment"><div class="line"># Download and unpack googletest at configure time</div>
<div class="line">configure_file(CMakeLists.txt.in googletest-download/CMakeLists.txt)</div>
<div class="line">execute_process(COMMAND ${CMAKE_COMMAND} -G &quot;${CMAKE_GENERATOR}&quot; .</div>
<div class="line">  RESULT_VARIABLE result</div>
<div class="line">  WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/googletest-download )</div>
<div class="line">if(result)</div>
<div class="line">  message(FATAL_ERROR &quot;CMake step for googletest failed: ${result}&quot;)</div>
<div class="line">endif()</div>
<div class="line">execute_process(COMMAND ${CMAKE_COMMAND} --build .</div>
<div class="line">  RESULT_VARIABLE result</div>
<div class="line">  WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/googletest-download )</div>
<div class="line">if(result)</div>
<div class="line">  message(FATAL_ERROR &quot;Build step for googletest failed: ${result}&quot;)</div>
<div class="line">endif()</div>
<div class="line"> </div>
<div class="line"># Prevent overriding the parent project&#39;s compiler/linker</div>
<div class="line"># settings on Windows</div>
<div class="line">set(gtest_force_shared_crt ON CACHE BOOL &quot;&quot; FORCE)</div>
<div class="line"> </div>
<div class="line"># Add googletest directly to our build. This defines</div>
<div class="line"># the gtest and gtest_main targets.</div>
<div class="line">add_subdirectory(${CMAKE_CURRENT_BINARY_DIR}/googletest-src</div>
<div class="line">                 ${CMAKE_CURRENT_BINARY_DIR}/googletest-build</div>
<div class="line">                 EXCLUDE_FROM_ALL)</div>
<div class="line"> </div>
<div class="line"># Now simply link against gtest or gtest_main as needed. Eg</div>
<div class="line">add_executable(example example.cpp)</div>
<div class="line">target_link_libraries(example gtest_main)</div>
<div class="line">add_test(NAME example_test COMMAND example)</div>
</div><!-- fragment --><p>Note that this approach requires CMake 2.8.2 or later due to its use of the <code>ExternalProject_Add()</code> command. The above technique is discussed in more detail in <a href="http://crascit.com/2015/07/25/cmake-gtest/">this separate article</a> which also contains a link to a fully generalized implementation of the technique.</p>
<h4><a class="anchor" id="autotoc_md269"></a>
Visual Studio Dynamic vs Static Runtimes</h4>
<p>By default, new Visual Studio projects link the C runtimes dynamically but GoogleTest links them statically. This will generate an error that looks something like the following: gtest.lib(gtest-all.obj) : error LNK2038: mismatch detected for 'RuntimeLibrary': value 'MTd_StaticDebug' doesn't match value 'MDd_DynamicDebug' in main.obj</p>
<p>GoogleTest already has a CMake option for this: <code>gtest_force_shared_crt</code></p>
<p>Enabling this option will make gtest link the runtimes dynamically too, and match the project in which it is included.</p>
<h3><a class="anchor" id="autotoc_md270"></a>
C++ Standard Version</h3>
<p>An environment that supports C++11 is required in order to successfully build GoogleTest. One way to ensure this is to specify the standard in the top-level project, for example by using the <code>set(CMAKE_CXX_STANDARD 11)</code> command. If this is not feasible, for example in a C project using GoogleTest for validation, then it can be specified by adding it to the options for cmake via the <code>DCMAKE_CXX_FLAGS</code> option.</p>
<h2><a class="anchor" id="autotoc_md271"></a>
Tweaking GoogleTest</h2>
<p>GoogleTest can be used in diverse environments. The default configuration may not work (or may not work well) out of the box in some environments. However, you can easily tweak GoogleTest by defining control macros on the compiler command line. Generally, these macros are named like <code>GTEST_XYZ</code> and you define them to either 1 or 0 to enable or disable a certain feature.</p>
<p>We list the most frequently used macros below. For a complete list, see file <a href="https://github.com/google/googletest/blob/master/googletest/include/gtest/internal/gtest-port.h">include/gtest/internal/gtest-port.h</a>.</p>
<h2><a class="anchor" id="autotoc_md272"></a>
Multi-threaded Tests</h2>
<p>GoogleTest is thread-safe where the pthread library is available. After <code>#include "gtest/gtest.h"</code>, you can check the <code>GTEST_IS_THREADSAFE</code> macro to see whether this is the case (yes if the macro is <code>#defined</code> to 1, no if it's undefined.).</p>
<p>If GoogleTest doesn't correctly detect whether pthread is available in your environment, you can force it with </p><pre class="fragment">-DGTEST_HAS_PTHREAD=1
</pre><p> or </p><pre class="fragment">-DGTEST_HAS_PTHREAD=0
</pre><p> When GoogleTest uses pthread, you may need to add flags to your compiler and/or linker to select the pthread library, or you'll get link errors. If you use the CMake script, this is taken care of for you. If you use your own build script, you'll need to read your compiler and linker's manual to figure out what flags to add.</p>
<h2><a class="anchor" id="autotoc_md273"></a>
As a Shared Library (DLL)</h2>
<p>GoogleTest is compact, so most users can build and link it as a static library for the simplicity. You can choose to use GoogleTest as a shared library (known as a DLL on Windows) if you prefer.</p>
<p>To compile <em>gtest</em> as a shared library, add </p><pre class="fragment">-DGTEST_CREATE_SHARED_LIBRARY=1
</pre><p> to the compiler flags. You'll also need to tell the linker to produce a shared library instead - consult your linker's manual for how to do it.</p>
<p>To compile your <em>tests</em> that use the gtest shared library, add </p><pre class="fragment">-DGTEST_LINKED_AS_SHARED_LIBRARY=1
</pre><p> to the compiler flags.</p>
<p>Note: while the above steps aren't technically necessary today when using some compilers (e.g. GCC), they may become necessary in the future, if we decide to improve the speed of loading the library (see <a href="http://gcc.gnu.org/wiki/Visibility">http://gcc.gnu.org/wiki/Visibility</a> for details). Therefore you are recommended to always add the above flags when using GoogleTest as a shared library. Otherwise a future release of GoogleTest may break your build script.</p>
<h2><a class="anchor" id="autotoc_md274"></a>
Avoiding Macro Name Clashes</h2>
<p>In C++, macros don't obey namespaces. Therefore two libraries that both define a macro of the same name will clash if you <code>#include</code> both definitions. In case a GoogleTest macro clashes with another library, you can force GoogleTest to rename its macro to avoid the conflict.</p>
<p>Specifically, if both GoogleTest and some other code define macro FOO, you can add </p><pre class="fragment">-DGTEST_DONT_DEFINE_FOO=1
</pre><p> to the compiler flags to tell GoogleTest to change the macro's name from <code>FOO</code> to <code>GTEST_FOO</code>. Currently <code>FOO</code> can be <code>FAIL</code>, <code>SUCCEED</code>, or <code>TEST</code>. For example, with <code>-DGTEST_DONT_DEFINE_TEST=1</code>, you'll need to write </p><pre class="fragment">GTEST_TEST(SomeTest, DoesThis) { ... }
</pre><p> instead of </p><pre class="fragment">TEST(SomeTest, DoesThis) { ... }
</pre><p> in order to define a test. </p>
</div></div><!-- contents -->
</div><!-- PageDoc -->
<!-- start footer part -->
<hr class="footer"/><address class="footer"><small>
Generated by&#160;<a href="https://www.doxygen.org/index.html"><img class="footer" src="doxygen.svg" width="104" height="31" alt="doxygen"/></a> 1.9.2
</small></address>
</body>
</html>
